package cn.iocoder.yudao.module.dt.controller.admin.activitywrite;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dt.controller.admin.activitywrite.vo.*;
import cn.iocoder.yudao.module.dt.dal.dataobject.activitywrite.ActivityWriteDO;
import cn.iocoder.yudao.module.dt.convert.activitywrite.ActivityWriteConvert;
import cn.iocoder.yudao.module.dt.service.activitywrite.ActivityWriteService;

@Api(tags = "管理后台 - 活动核销记录")
@RestController
@RequestMapping("/dt/activity-write")
@Validated
public class ActivityWriteController {

    @Resource
    private ActivityWriteService activityWriteService;

    @PostMapping("/create")
    @ApiOperation("创建活动核销记录")
    @PreAuthorize("@ss.hasPermission('dt:activity-write:create')")
    public CommonResult<Integer> createActivityWrite(@Valid @RequestBody ActivityWriteCreateReqVO createReqVO) {
        return success(activityWriteService.createActivityWrite(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新活动核销记录")
    @PreAuthorize("@ss.hasPermission('dt:activity-write:update')")
    public CommonResult<Boolean> updateActivityWrite(@Valid @RequestBody ActivityWriteUpdateReqVO updateReqVO) {
        activityWriteService.updateActivityWrite(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除活动核销记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dt:activity-write:delete')")
    public CommonResult<Boolean> deleteActivityWrite(@RequestParam("id") Integer id) {
        activityWriteService.deleteActivityWrite(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得活动核销记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dt:activity-write:query')")
    public CommonResult<ActivityWriteRespVO> getActivityWrite(@RequestParam("id") Integer id) {
        ActivityWriteDO activityWrite = activityWriteService.getActivityWrite(id);
        return success(ActivityWriteConvert.INSTANCE.convert(activityWrite));
    }

    @GetMapping("/list")
    @ApiOperation("获得活动核销记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('dt:activity-write:query')")
    public CommonResult<List<ActivityWriteRespVO>> getActivityWriteList(@RequestParam("ids") Collection<Integer> ids) {
        List<ActivityWriteDO> list = activityWriteService.getActivityWriteList(ids);
        return success(ActivityWriteConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得活动核销记录分页")
    @PreAuthorize("@ss.hasPermission('dt:activity-write:query')")
    public CommonResult<PageResult<ActivityWriteRespVO>> getActivityWritePage(@Valid ActivityWritePageReqVO pageVO) {
        PageResult<ActivityWriteDO> pageResult = activityWriteService.getActivityWritePage(pageVO);
        return success(ActivityWriteConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出活动核销记录 Excel")
    @PreAuthorize("@ss.hasPermission('dt:activity-write:export')")
    @OperateLog(type = EXPORT)
    public void exportActivityWriteExcel(@Valid ActivityWriteExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ActivityWriteDO> list = activityWriteService.getActivityWriteList(exportReqVO);
        // 导出 Excel
        List<ActivityWriteExcelVO> datas = ActivityWriteConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "活动核销记录.xls", "数据", ActivityWriteExcelVO.class, datas);
    }

}
